<template>
  <div class="app-page">
    <div class="table">
      <el-table-form ref="form">
        <el-form-item class="merge-item">
          <el-select v-model="qtype" style="width: 180px; margin-right: 2px;">
            <el-option label="客户编号" value="csmCode" />
            <el-option label="客户名称" value="csmName" />
            <el-option label="充值卡号" value="cardCode" />
          </el-select>
          <el-input v-model.trim="qtTxt" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="充值机构:">
          <el-tree-select
            v-model="query.orgSNNo"
            :clearable="false"
            class="tree-select"
            :popover-width="360"
            :select-params="{
              clearable: false,
              placeholder: '请选择责任机构',
            }"
            :tree-params=" {
              'expand-on-click-node': false,
              'highlight-current': true,
              props:{
                value:'orgSNNo',
                label: 'orgName',
                disabled: 'disabled',
              },
              data: treeData
            }"
          />
        </el-form-item>
        <el-form-item label="充值日期:" :table-form-merge="2">
          <el-date-picker
            v-model="date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            unlink-panels
          />
        </el-form-item>
        <el-form-item label="充值类型:">
          <el-select v-model="query.rcType" clearable>
            <el-option label="储值卡" :value="1" />
            <el-option label="计数卡" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态:">
          <el-select v-model="query.reviewType" clearable>
            <el-option label="待审核" :value="1" />
            <el-option label="驳回" :value="3" />
          </el-select>
        </el-form-item>
        <template v-slot:btns>
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </template>
      </el-table-form>
      <lb-table
        ref="lbTable"
        v-loading="$loadingPlugin.fetchData"
        v-el-height-adaptive-table="{bottomOffset:10}"
        border
        highlight-current-row
        element-loading-text="加载中"
        :column="column"
        :data="tableData.data"
        pagination
        :total="tableData.total"
        custom-column
        :row-class-name="seRowClass"
        @fetch="fetchData"
      />
    </div>
    <DepositAudit ref="deposit" @upDataCheckTable="upDataCheckTable" />
  </div>
</template>

<script>
  import { parseTime } from '@/utils'
  import { topUpAdministrationList, topUpAdministrationInfo } from '@/api'
  import DepositAudit from './DepositAudit.vue'
  import moment from 'dayjs'

  export default {
    name: 'top-up-check',
    components: {
      DepositAudit
    },
    props: {
      treeData: {
        type: Array,
        required: true
      }
    },
    data() {
      return {
        query: {
          rcType: '',
          reviewType: '',
          orgSNNo: ''
        },
        qtTxt: '',
        qtype: 'csmCode',
        date: [],
        tableData: {
          total: 0,
          data: []
        }
      }
    },
    computed: {
      column: function() {
        return [
          {
            prop: 'cardCode',
            label: '充值卡号',
            width: 150
          },
          {
            prop: 'reviewType',
            label: '状态',
            width: 150,
            render: (h, { row }) => <span>{row.reviewType && this.filtersType(row.reviewType) }</span>
          },

          {
            prop: 'rcType',
            label: '充值类型',
            width: 150,
            render: (h, { row }) => <span>{row.rcType && this.filtersTopUpType(row.rcType) }</span>
          },
          {
            prop: 'csmCode',
            label: '绑定客户编号',
            width: 150
          },
          {
            prop: 'csmName',
            label: '绑定客户名称',
            width: 150
          },
          {
            prop: 'singleMoney',
            label: '充值金额',
            width: 100,
            render: (h, { row }) => <span>{row.singleMoney && this.numberFormat1(row.singleMoney)}</span>
          },
          {
            prop: 'createTime',
            label: '充值日期',
            width: 150,
            render: (h, { row }) => <span>{ row.createTime && parseTime(row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }</span>
          },
          {
            prop: 'createUser',
            label: '充值人',
            width: 100
          },
          {
            prop: 'orgName',
            label: '充值机构',
            width: 150
          },
          {
            prop: 'remark',
            label: '备注',
            width: 150
          },
          {
            label: '操作',
            align: 'center',
            fixed: 'right',
            render: (h, { row, $index }) => {
              return (
                <div>
                  { row.reviewType === 1 && <el-link v-permission='17' size='mini' onClick={() => this.onCheck(row)}>审核</el-link> }
                </div>
              )
            }
          }
        ]
      }
    },
    async created() {},
    methods: {
      async fetchData({ currentPage, pageSize }) {
        const params = {
          ...this._getQuery(this.query),
          pageIndex: currentPage,
          pageSize
        }
        params[this.qtype] = this.qtTxt
        if (this.date && this.date.length === 2) {
          params.startTime = moment(this.date[0]).format()
          params.endTime = moment(this.date[1]).format()
        } else {
          params.startTime = null
          params.endTime = null
        }
        if (!this.query.reviewType) params.reviewType = -1
        const res = await topUpAdministrationList(params)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      // 审核
      async onCheck(row) {
        const res = await topUpAdministrationInfo({ recordId: row.recordId })
        this.$refs.deposit.open(res.data, row.recordId)
      },
      async upDataCheckTable() {
        await this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 清除对象空属性
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      // 查询
      async search() {
        await this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 状态
      filtersType(r) {
        const obj = {
          1: '待审核',
          3: '驳回'
        }
        return obj[r]
      },
      // 充值类型
      filtersTopUpType(r) {
        const obj = {
          1: '储值卡',
          2: '计数卡'
        }
        return obj[r]
      },
      // 充值金额
      numberFormat1: function(val) {
        val = val * 1.00 / 100
        val = Number(val)
        val = isNaN(val) ? '0.00' : val.toFixed(2)
        return val + ' 元'
      },
      seRowClass({ row, rowIndex }) {
        if (row.reviewType === 3) return 'red-bg-row'
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
